/*
 * Copyright 2013-2020 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.cloud.gateway.config;

import reactor.core.publisher.Flux;

import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.cloud.gateway.route.RouteDefinitionLocator;

/**
 * @author Spencer Gibb
 *
 * PropertiesRouteDefinitionLocator 是 Spring Cloud Gateway 中的一个核心组件，
 * 用于从配置文件（如 application.yml 或 application.properties）中加载路由定义。
 * 它是实现动态路由的一种方式，允许开发者通过外部化配置来定义网关的路由规则。
 *
 * 扩展：配合 CompositeRouteLocator 使用
 * 		在实际项目中，通常会有多个 RouteDefinitionLocator 实例，例如：
 * 		PropertiesRouteDefinitionLocator：从配置文件加载静态路由。
 * 		RedisRouteDefinitionLocator：从 Redis 加载动态路由。
 * 		DatabaseRouteDefinitionLocator：从数据库加载动态路由。
 * 		这些 RouteDefinitionLocator 可以通过 CompositeRouteLocator 组合在一起，形成一个统一的路由定位器。
 */
public class PropertiesRouteDefinitionLocator implements RouteDefinitionLocator {

	private final GatewayProperties properties;

	public PropertiesRouteDefinitionLocator(GatewayProperties properties) {
		this.properties = properties;
	}

	@Override
	public Flux<RouteDefinition> getRouteDefinitions() {
		return Flux.fromIterable(this.properties.getRoutes());
	}

}
